/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.java.controller;

import com.java.common.Encryption;
import com.java.editor.QuyenEditor;
import com.java.editor.TinhEditor;
import com.java.mapbean.Nguoidung;
import com.java.mapbean.Quyen;
import com.java.mapbean.Tinh;
import com.java.service.NguoiDungService;
import com.java.service.QuyenService;
import com.java.service.TheLoai1Service;
import com.java.service.TinhService;
import javax.servlet.http.HttpServletRequest;
import org.hibernate.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 *
 * @author quannq
 */
@Controller
public class UserController {

    private QuyenService quyenService = new QuyenService();
    private TheLoai1Service tl1Service = new TheLoai1Service();
    private TinhService tinhService = new TinhService();
    private NguoiDungService nguoidungService = new NguoiDungService();
    @Autowired
    private JavaMailSender mailSender;

    @InitBinder
    protected void initBinder(HttpServletRequest request,
            ServletRequestDataBinder binder) throws Exception {
        binder.registerCustomEditor(Quyen.class, new QuyenEditor(this.quyenService));
        binder.registerCustomEditor(Tinh.class, new TinhEditor(this.tinhService));
        request.setCharacterEncoding("UTF-8");
    }

    public void init(ModelMap model) {
        model.addAttribute("TheLoai1", tl1Service.getAll());
        model.addAttribute("Tinh", tinhService.getAll());
    }

    @RequestMapping(value = "logon", method = RequestMethod.POST)
    public String handleLogon(@ModelAttribute Nguoidung nguoidung, ModelMap model) {
        nguoidung.setPassword(Encryption.encrypt(nguoidung.getPassword()));
        nguoidung.setAvatar(nguoidung.getDanhxung()==1?"woman.png":"man.png");
        try {
            nguoidungService.saveNguoiDung(nguoidung);
            SimpleMailMessage email = new SimpleMailMessage();
            email.setTo(nguoidung.getUsername());
            email.setSubject("KÍCH HOẠT TÀI KHOẢN");
            email.setText("http://localhost:8080/bookshop/active/"+ nguoidung.getUsername() + "/" + nguoidung.getPassword());
            mailSender.send(email);
            model.addAttribute("logon", "true");
            return "redirect:/login";
        } catch (Exception e) {
            model.addAttribute("false", "false");
            return "redirect:logon";
        }

        // init(model);
        // model.addAttribute("contentpage", "Logon.jsp");

    }

    @RequestMapping(value = "logon", method = RequestMethod.GET)
    public String logonUser(ModelMap model) {
        init(model);
        model.addAttribute("nguoidung", new Nguoidung());
        model.addAttribute("contentpage", "Logon.jsp");
        return "User";
    }

    @RequestMapping(value = "active/{email}/{code}", method = RequestMethod.GET)
    public String activeUser(@PathVariable(value = "email") String email,
            @PathVariable(value = "code") String code, ModelMap model) {
        Nguoidung nguoidung = nguoidungService.findUserName(email);
        System.out.println(nguoidung.getUsername());
        if (nguoidung.getPassword().equals(code)) {
            nguoidung.setStatus(1);
            nguoidungService.updateNguoiDung(nguoidung);
            model.addAttribute("active", "true");
            return "redirect:/login";
        }
        return "/";
    }
//    @RequestMapping("test")
//    public void test(HttpServletRequest request){
//        System.out.println(request.getContextPath());
//        System.out.println(request.getLocalAddr());
//        System.out.println(request.getRealPath("/image"));
//    }
}
